package service

import (
	"good.house/internal/dto/request"
	"good.house/internal/model"
	"good.house/internal/repository"
)

type PublishService struct {
	repo repository.IPublishRepository
}

func NewPublishService(repo repository.IPublishRepository) *PublishService {
	return &PublishService{
		repo: repo,
	}
}

func (s *PublishService) Publish(req *request.PublishRequest, userId int64) error {
	resource := make([]model.ResourceData, len(req.Resource))

	for i, item := range req.Resource {
		resource[i] = model.ResourceData{
			Type: item.Type,
			Path: item.Path,
		}
	}

	publishInfo := &model.Publish{
		UserId:       userId,
		Title:        req.Title,
		Content:      req.Content,
		MonthRent:    req.MonthRent,
		SeasonalRent: req.SeasonalRent,
		HalfYearRent: req.HalfYearRent,
		Resource:     resource,
		Longitude:    req.Longitude,
		Latitude:     req.Latitude,
		Address:      req.Address,
		Tag:          req.Tag,
		CompleteSet:  req.CompleteSet,
	}

	if req.ID != 0 {
		// 修改
		publishInfo.ID = req.ID
		return s.repo.Update(publishInfo)
	} else {
		// 创建
		return s.repo.Create(publishInfo)
	}
}
